<?php
namespace app\jiyun\controller\cargo;

use app\common\model\admin\service_config\ServiceConfigTabAccount;
use app\common\model\collect\OrderExpressGoodsModel;
use app\common\model\collect\OrderExpressModel;
use app\common\model\jiyun\base\ChannelModel;
use app\common\model\jiyun\OrderModel;
use app\common\model\jiyun\warehouse\WarehouseModel;
use app\common\model\jiyun\warehouse\WarehouseRegionLocationModel;
use think\admin\Controller;
use think\facade\Validate;

/**
 * 扫描入库
 * @Description
 * @package app\jiyun\controller\cargo
 * @author xuli
 * @date 2024-12-04 10:22
 */
class ScanStore extends Controller
{
    /**
     * 扫描入库
     * @auth true
     * @menu true
     */
    public function index()
    {
        $this->fetch();
    }

    /**
     * 搜索订单信息
     */
    public function searchOrder()
    {
        $express_no = $this->request->get('express_no');
        if(empty($express_no)){
            $this->error('请输入快递单号或者合箱单号');
        }

        // 查询拣货单信息
        $map = [
            ['express_no', '=', $express_no]
        ];
        $pack_info = OrderExpressModel::mk()->where($map)->find();
        if(empty($pack_info)){
            $this->error('快递单号不存在');
        }
        if($pack_info['status'] != OrderExpressModel::STATUS_YES_CHECK) {
            $this->error('快递单号状态错误');
        }

        // 获取仓库信息
        $Warehouse = WarehouseModel::mk()->find($pack_info['warehouse_id']);
        $pack_info['warehouse_name'] = $Warehouse['warehouse_c_name'] ?? '';

        // 获取货位列表
        $locationList = WarehouseRegionLocationModel::mk()->where(['warehouse_id' => $pack_info['warehouse_id'], 'status' => 20])->column('location_code','id');

        $this->success('',['location_list' => $locationList, 'pack_info' => $pack_info]);

    }

    /**
     * 获取包裹商品列表
     * @return \think\response\Json
     */
    public function getExpressGoodsList()
    {
        $express_id = $this->request->get('express_id', 0);
        if(!$express_id) {
            return json(['code' => 0, 'msg' => '', 'data' => []]);
        }
        // 获取快递包裹信息
        $info = OrderExpressModel::mk()->find($express_id);
        if(empty($info)) {
            return json(['code' => 0, 'msg' => '', 'data' => []]);
        }
        // 获取商品列表
        $list = OrderExpressGoodsModel::mk()->where(['order_express_id' => $express_id])->select();
        return json(['code' => 0, 'msg' => '', 'data' => $list]);
    }

    /**
     * 快递包裹列表
     * @auth true
     */
    public function confirmStore()
    {
        $express_id = $this->request->post('express_id', 0);
        $location_code = $this->request->post('location_code', 0);

        if(empty($express_id)) {
            $this->error(lang('包裹信息错误'));
        }
        if(empty($location_code)) {
            $this->error(lang('请选择货位'));
        }

        // 获取
        $express_info = OrderExpressModel::mk()->find($express_id);
        if(empty($express_info)) {
            $this->error(lang('包裹信息错误'));
        }
        if($express_info['status'] != OrderExpressModel::STATUS_YES_CHECK) {
            $this->error(lang('包裹未签收或者已入库'));
        }
        $result = OrderExpressModel::mq()->where(['id' => $express_info['id']])->update(['status' => OrderExpressModel::STATUS_IN_STORE]);
        if($result) {
            $this->success(lang('入库成功'));
        } else {
             $this->error(lang('入库失败'));
        }
    }
}